#! /usr/bin/python

class SimpleClass:
    field = 1
    count = 0
    def __init__(self):
        self.haha = "haha"
        self.__class__.count = self.__class__.count + 1
        print "the construction"

print SimpleClass.count
a = SimpleClass()
Aclass = SimpleClass
b = Aclass()
print "simpleclass has ", b.count, "object"
print SimpleClass.__doc__
print vars(SimpleClass)
print dir(SimpleClass)
print vars(a)
print dir(a)

#a.field = 2
setattr(a, "field", 2)
print vars(a)
del a.field
print vars(a)
if hasattr(a, "field"):
    print "a has attribute \"field\""
else:
    print "a has no attribute filed"
attributename = "field"

print getattr(a, attributename)

obj = SimpleClass()
SimpleClass.a = 100
print obj.a
SimpleClass.b = 101
print obj.b
print vars(obj)

obj1 = SimpleClass()
print obj1.haha
print obj1.count

class SClass (SimpleClass):
    def __init__(self):
        print "inherited from simpleclass"
    __private_attr1 = 100
    ___private_attr2_ = 101
    def __repr__(self):
        return "SClass()"
    def __str__(self):
        return "hello an inherited class"
    def __cmp__(self, other):
        if other > 0: return 1;
        elif other < 0: return -1;
        else: return 0
    def __hash__(self):
        return 0
    alive = 0;
    def __nonzero__(self):
        if self.alive == 0: return 0
        else: return 1
    def __len__(self):
        return 100
    def __contains__(self, other):
        if other=="ok": return 1
        return 0
    #use object like function
    def __call__(self, *arg):
        for k in arg:
            print k
    def __getitem__(self, key):
        print "key is %s"%key
        if key >=0 and key<=10:return key*key;
        if key == "bog": return 43.432
        elif key == "depth": return 1000
        else: raise IndexError
    def __setitem__(self, key, value):
        print "debug:you want to set [%s] to [%s]"\
                %(key, value)
    def __delitem__(self, key):
        print "debug: you want to delete [%s]"%(key,)
    def __getslice__(self, i, j):
        return 0



print SClass.__bases__
obj6 = SClass()
print SimpleClass.count
print vars(SClass)
print SClass._SClass__private_attr1

oper_over = SClass()
print repr(SClass)
print str(SClass)
print repr(oper_over)
print str(oper_over)

SClassobj = eval(repr(oper_over))
print SClassobj
if SClassobj > -10 :print "ok"
if SClassobj < -10: print "ok less then"
print SClassobj.alive
if SClassobj: print "alive"
else: print "not alive"
print len(SClassobj)
print SClassobj["bog"]
print SClassobj["depth"]
for k in SClassobj: print k

def simulatefor(a):
    idx = 0
    while (1):
        try:
            k = a[idx]
            print k
            idx = idx +1
        except IndexError:
            break
simulatefor(SClassobj)

SClassobj["key"] = "value"
del SClassobj["key"]
print SClassobj[:]
#test __contains__
if "ok" in SClassobj: print "ok in a"
else: print "ok is not in a"

SClassobj("hello", "world")
